package com.campus.merchant.controller;

import com.campus.common.bean.common.Resp;
import com.campus.merchant.bean.apply.MerchantApplyBean;
import com.campus.merchant.bean.merchant.MerchantStatusUpdateBean;
import com.campus.merchant.bean.merchant.MerchantUpdateBean;
import com.campus.merchant.service.MerchantService;
import com.campus.merchant.vo.merchant.MerchantApplyVO;
import com.campus.merchant.vo.merchant.MerchantVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 商户表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    /**
     * 查询商户入驻状态
     * 
     * @param merchantUserId 商家用户ID
     * @return 状态、拒绝原因
     */
    @GetMapping("/entry/status")
    public Resp getEntryStatus(@RequestParam("merchantUserId") Integer merchantUserId) {
        Map<String, Object> result = merchantService.getEntryStatus(merchantUserId);
        return Resp.success(result);
    }

    /**
     * 查询商户最新入驻申请记录
     *
     * @param merchantUserId 商家用户ID
     * @return 最新入驻申请VO
     */
    @GetMapping("/entry/latest")
    public Resp<MerchantApplyVO> getLatestEntryApply(@RequestParam("merchantUserId") Integer merchantUserId) {
        MerchantApplyVO latest = merchantService.getLatestEntryApply(merchantUserId);
        return Resp.success(latest);
    }

    /**
     * 提交商户入驻申请
     * 
     * @param bean 入驻表单Bean
     * @return 提交结果
     */
    @PostMapping("/entry/apply")
    public Resp submitMerchantApply(@RequestBody MerchantApplyBean bean) {
        return merchantService.submitMerchantApply(bean);
    }

    /**
     * 根据商家ID查询商家信息
     * @param merchantId 商家ID
     * @return 商家信息VO
     */
    @GetMapping("/info")
    public Resp<MerchantVO> getMerchantInfo(@RequestParam("merchantId") Integer merchantId) {
        MerchantVO vo = merchantService.getMerchantById(merchantId);
        return Resp.success(vo);
    }

    /**
     * 更新商家信息
     * @param merchantUpdateBean 商家更新信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public Resp updateMerchantInfo(@RequestBody MerchantUpdateBean merchantUpdateBean) {
        merchantService.updateMerchantInfo(merchantUpdateBean);
        return Resp.success();
    }

    /**
     * 更新商户营业状态
     * @param bean 商户营业状态更新参数
     * @return 更新结果
     */
    @PostMapping("/updateStatus")
    public Resp updateMerchantStatus(@Valid @RequestBody MerchantStatusUpdateBean bean) {
        merchantService.updateMerchantStatus(bean);
        return Resp.success();
    }
}
